package jaasConfig;

import java.io.IOException;
import java.util.Map;

import javax.security.auth.Subject;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.login.LoginException;
import javax.security.auth.spi.LoginModule;

public class RdbmsLoginModule  implements LoginModule  {

	private Subject subject;
	private CallbackHandler callbackHandler;
	private Map<String, ?> sharedState;
	private Map<String, ?> options;
	private String url;
	private String driverClass;
	private boolean debug;
	private Callback[] callbacks;
	private boolean success;

	//LoginContext在调用login（）方法时会调用initialize（）方法。RdbmsLoginModule的第一个任务就是在类中保存输入参数的引用。在验证成功后将向Subject对象中送入Principal对象和凭证。
	@Override
	public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState,
			Map<String, ?> options) {
		this.subject = subject;
		this.callbackHandler = callbackHandler;
		this.sharedState = sharedState;
		this.options = options;
		 
		url = (String)options.get("url");
		driverClass = (String)options.get("driver");
		debug = "true".equalsIgnoreCase((String)options.get("debug"));
	}

	@Override
	public boolean login() throws LoginException {
		if (callbackHandler == null)
			throw new LoginException("no handler");
			NameCallback nameCb = new NameCallback("user: ");
			PasswordCallback passCb = new PasswordCallback("password: ", true);
			callbacks = new Callback[] { nameCb, passCb };
			try {
				callbackHandler.handle(callbacks);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedCallbackException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String username = nameCb.getName();
			String password = new String(passCb.getPassword());
			System.out.println(username+"___"+password);
			success = rdbmsValidate(username, password);
			return success ;
	}

	private boolean rdbmsValidate(String username, String password) {
		
		
		
		return true;
	}

	@Override
	public boolean commit() throws LoginException {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean abort() throws LoginException {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean logout() throws LoginException {
		// TODO Auto-generated method stub
		return true;
	}


}
